package com.example.examapp;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Toast;

public class FragmentThree extends Fragment {

    private Chronometer chronometer;
    private Button btn_start, btn_stop, btn_base, btn_format;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getContext(), R.layout.activity_fragment_three, null);

        chronometer = view.findViewById(R.id.chronometer);
        btn_start = view. findViewById(R.id.btnStart);
        btn_stop = view. findViewById(R.id.btnStop);
        btn_base = view. findViewById(R.id.btnReset);
        btn_format = view. findViewById(R.id.btn_format);

        initView();
        return view;
    }

    private void initView() {

        chronometer.setOnChronometerTickListener((Chronometer.OnChronometerTickListener) onClick);
        btn_start.setOnClickListener(onClick);
        btn_stop.setOnClickListener(onClick);
        btn_base.setOnClickListener(onClick);
        btn_format.setOnClickListener(onClick);

    }


    public View.OnClickListener onClick=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btnStart:
                    chronometer.start();// 开始计时
                    break;
                case R.id.btnStop:
                    chronometer.stop();// 停止计时
                    break;
                case R.id.btnReset:
                    chronometer.setBase(SystemClock.elapsedRealtime());// 复位
                    break;
                case R.id.btn_format:
                    chronometer.setFormat("T：%s");// 更改时间显示格式
                    break;
            }
        }
    };
//    @Override
//    public void onChronometerTick(Chronometer chronometer) {
//        String time = chronometer.getText().toString();
//        if(time.equals("00:00")){
//            Toast.makeText(FragmentThree.this,"时间到了~", Toast.LENGTH_SHORT).show();
//        }
//    }

}